package com.jeeplus.common.utils.concurrent.threadpool;

import java.util.concurrent.*;

/**
 * task线程池的工具类(单例)13  *
 * 2020年02月26日
 * @param
 */
public class MyThreadPoolExecutor {
    // Sets the amount of time an idle thread will wait for a task before terminating
    private static final int KEEP_ALIVE_TIME = 1;

    // Sets the Time Unit to seconds
    private static final TimeUnit KEEP_ALIVE_TIME_UNIT;

    // Sets the initial threadpool size to 8
    private static final int CORE_POOL_SIZE = 8;

    // Sets the maximum threadpool size to 8
    private static final int MAXIMUM_POOL_SIZE = 8;    // A static block that sets class fields

    private final BlockingQueue<Runnable> MyBlockingQueue;

    // A managed pool of background download threads
    private final ThreadPoolExecutor MyPoolExecutor;

    // A single instance of PhotoManager, used to implement the singleton pattern
    private static MyThreadPoolExecutor MyThreadPoolInstance = null;

    static {
        // The time unit for "keep alive" is in seconds
        KEEP_ALIVE_TIME_UNIT = TimeUnit.SECONDS;

        // Creates a single static instance of PhotoManager
        MyThreadPoolInstance = new MyThreadPoolExecutor();
    }


    private MyThreadPoolExecutor() {
        MyBlockingQueue = new LinkedBlockingQueue<Runnable>();
        MyPoolExecutor = new ThreadPoolExecutor(CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE_TIME, KEEP_ALIVE_TIME_UNIT, MyBlockingQueue);
    }

    public MyThreadPoolExecutor(BlockingQueue<Runnable> myBlockingQueue, ThreadPoolExecutor myPoolExecutor) {
        MyBlockingQueue = myBlockingQueue;
        MyPoolExecutor = myPoolExecutor;
    }

    public static MyThreadPoolExecutor getInstance() {

        return MyThreadPoolInstance;
    }

    public void queueJob(Runnable r){

        MyPoolExecutor.execute(r);
    }

}
